//
//  GQRecommandTableCell.m
//  MyMusic
//
//  Created by sunny on 16/4/14.
//  Copyright © 2016年 sunny. All rights reserved.
//

#import "GQRecommandTableCell.h"
#import "GQTodayRecommandViewController.h"
#import "GQRecommandCellButton.h"
#import "GQRecommandTableViewItem.h"

@interface GQRecommandTableCell ()
@property (nonatomic, strong)NSMutableArray *buttonsArray;
@property (nonatomic, assign)BOOL isAddButton;

@end

@implementation GQRecommandTableCell

- (NSMutableArray *)buttonsArray
{
    if (_buttonsArray == nil) {
        _buttonsArray = [NSMutableArray array];
    }
    return _buttonsArray;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
//    布局按钮的位置和尺寸
    if (self.tableItemArray.count == 0|| self.tableItemArray.count > self.tableItemArray.count) return;
    NSInteger count = self.buttonsArray.count;
    CGFloat buttonW = self.gq_width / count;
    CGFloat buttonH = self.gq_height;
    CGFloat buttonX = 0;
    CGFloat buttonY = 0;
    for (NSInteger i = 0; i < count ; i ++) {
        buttonX = i * buttonW;
        GQRecommandCellButton *button = self.buttonsArray[i];
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        
//        设置按钮的属性
        GQRecommandTableViewItem *item = self.tableItemArray[i];
        [button setTitle:item.title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.centerLabel.text = item.day;
        button.font = [UIFont systemFontOfSize:15];
        UIImageView *imageView = button.imageView;
        [imageView sd_setImageWithURL:[NSURL URLWithString:item.icon]placeholderImage:[UIImage imageNamed:@"ad_play_cover_pic_bg"]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (image == nil) return;
            [button setImage:image forState:UIControlStateNormal];
        }];
        imageView = nil;

        
    }
}

- (void)setTableItemArray:(NSArray *)tableItemArray
{
    _tableItemArray = tableItemArray;

    if (self.isAddButton || tableItemArray.count == 0) return;
    
        for (NSInteger i = 0; i < tableItemArray.count; i ++) {
            GQRecommandCellButton *button = [[GQRecommandCellButton alloc]init];
            button.tag = i;
//            监听按钮的点击
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:button];
            [self.buttonsArray addObject:button];
            self.isAddButton = YES;
        }
   
    
}

- (void)setFrame:(CGRect)frame
{
    frame.size.height -= 2;
    [super setFrame:frame];
}

#pragma mark - 监听按钮的点击事件
- (void)buttonClick:(GQRecommandCellButton *)button
{
//    点击了今日推荐歌曲
    if (button.tag == 2) {
        UIViewController *Vc = self.window.rootViewController.childViewControllers[0].childViewControllers[0];
        GQTodayRecommandViewController *todayRecommandVc = [[UIStoryboard storyboardWithName:@"GQTodayRecommandViewController" bundle:nil] instantiateInitialViewController];
//        传模型给今日推荐模块
        todayRecommandVc.recommandTableViewItem = self.tableItemArray.lastObject;
        todayRecommandVc.hidesBottomBarWhenPushed = YES;
        [Vc.navigationController pushViewController:todayRecommandVc animated:YES];
    }
}
@end
